Skip to content

Add Process.Env - #930

Merged
talex5 merged 1 commit into
ocaml-multicore:mainfrom
talex5:process-env
Sep 5, 2026
Merged

Add Process.Env#930
talex5 merged 1 commit into
ocaml-multicore:mainfrom
talex5:process-env

Conversation

@talex5

@talex5 talex5 commented Sep 3, 2026

Copy link
Copy Markdown
Collaborator

This provides helper functions to get and set environment variables. For backwards compatibility the type is exposed, but having this module will make it easier to make the type abstract later.

(@avsm mentioned in #923 (comment) that we might want to change the type here at some point)

@patricoferris: this might be useful for #923.

@talex5 talex5 added the api API design decision label Sep 3, 2026
@talex5
talex5 force-pushed the process-env branch 2 times, most recently from d0a2f4e to 10b6113 Compare September 3, 2026 14:00
Comment thread lib_eio/process.ml

module M = Map.Make(String)

let override bindings t =

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a deviation from execve behaviour, which passes through duplicate bindings and lets glibc/musl/etc handle that. Not necessarily a bad thing though, I haven't investigated what the libcs do yet.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add some docs, but the idea is that the raw of_array API lets you do whatever you want (arbitrary array of strings), while the bindings APIs (that take lists of pairs) work like setenv (every entry added must be a key/value pair and duplicates are not created).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense. What about case sensitivity? I think windows end keys are insensitive

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I guess the easiest way would be to change the behaviour of Process.Env depending on the host OS. I wonder what encoding Windows assumes for a case-insensitive compare? Are names always ASCII there?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've pushed a commit that does case insensitive compares on Windows now. It's not very efficient, but there aren't usually many variables anyway.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change looks good, but I really dislike the portable interface varying behaviour based on which host it's running on. Here's a radical idea: why not specify our Eio environment interface as explicitly being case insensitive? We are already constraining it to forbid duplicates, and it seems like normalising on case should also be very safe. We could also preserve the case at the Eio level (so it's passed through as-is) but is case-insensitive for comparisons.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Being case insensitive in general seems bad: it's a surprising change to the expected behaviour, and it causes trouble with non-ascii encodings.

The other option is to track whether a particular environment is Windows-style or not. But we can't use a flag at the moment because the type needs to be string array. It would be possible to track the type with a fake entry at the start (EIO_OS=Windows or something, that gets stripped out in to_array). Ugly, though.

A simpler solution is to recommend that environment variable names are upper-case (which they mostly are anyway). As long as all variables are uppercase, the Windows and POSIX behaviours are the same anyway.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A simpler solution is to recommend that environment variable names are upper-case (which they mostly are anyway). As long as all variables are uppercase, the Windows and POSIX behaviours are the same anyway.

agreed! good idea

Comment thread lib_eio/process.ml
Comment thread lib_eio/process.mli
This provides helper functions to get and set environment variables.
For backwards compatibility the type is exposed, but having this module
will make it easier to make the type abstract later.

Co-authored-by: Anil Madhavapeddy <anil@recoil.org>
@talex5
talex5 merged commit 0ee73e4 into ocaml-multicore:main Sep 5, 2026
4 of 5 checks passed
@talex5
talex5 deleted the process-env branch September 5, 2026 10:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api API design decision

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants